package com.sakura.thread.chapter11;

import java.util.concurrent.*;

/**
 * Created by apple on 2018/12/24.
 */
public class FutureTest {
    private final static ThreadPoolExecutor executorService = new ThreadPoolExecutor(1,1,1L, TimeUnit.MINUTES,
            new ArrayBlockingQueue<Runnable>(1), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Future futureOne = executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("start  runnable one");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Future futureTwo = executorService.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("start  runnable two");
            }
        });

        Future futureThree = null;
        try{
            futureThree = executorService.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("start  runnable three");
                }
            });
        }catch (Exception e){
            System.out.println(e.getLocalizedMessage());
        }

        System.out.println("task one " + futureOne.get());
        System.out.println("task two " + futureTwo.get());
        System.out.println("task three " + futureThree == null ? null : futureThree.get());

        executorService.shutdown();

    }
}
